//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    current: 0,
    tabBar: [{
        "pagePath": "../index/index",
        "text": "首页",
        "iconPath": "/images/index.png",
        "selectedIconPath": "/images/index_h.png",
      },
      {
        "pagePath": "../fastCardApply/fastCardApply",
        "text": "快速开卡",
        "iconPath": "/images/card.png",
        "selectedIconPath": "/images/card_h.png",
      },
      {
        "pagePath": "../user/user",
        "text": "我的",
        "iconPath": "/images/user.png",
        "selectedIconPath": "/images/user_h.png",
      }
    ],

    userIcon: {
      message: '/images/message_icon.png',
      setIcon: '/images/set_icon.png',
      headIcon: '/images/head.png',
      bg_top: '/images/bg_top.png',
    },
    navList: [{
        id: 2,
        icon: '/images/icon_cardRecharge.png',
        name: '水卡充值',
        url: '../fastCardApply/fastCardApply?from=2',
        status: true
      },
      {
        id: 6,
        icon: '/images/icon_cardManagement.png',
        name: '水卡管理',
        url: '../card/card',
        status: true
      },
      {
        id: 7,
        icon: '/images/icon_performanceManagement.png',
        name: '业绩管理',
        url: '../capitalmanage/capitalmanage',
        status: true
      },
      {
        id: 10,
        icon: '/images/icon_accountManagement.png',
        name: '账户管理',
        url: '../account/account',
        status: true
      },
      {
        id: 16,
        icon: '/images/icon_sellCard.png',
        name: '充值卡',
        url: '../../../package/pages/sellCard/list/list',
        status: true
      },
      {
        id: 15,
        icon: '/images/icon-e-wait.png',
        name: '版本更新',
        url: '../version/list/list',
        status: true
      }
    ],
    bar_icon: '/images/bar_icon.png',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    is_new: 1,
    status: 0,
    index_data: [],
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {

    var that = this;
    var userInfo = wx.getStorageSync('userInfo');
    if (userInfo) {
      that.setData({
        userInfo: userInfo,
        hasUserInfo: true
      });
      if (that.data.userInfo.superior_id != 0) {
        that.setData({
          'navList[10].status': false
        });
      }
    } else if (that.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        that.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          that.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onShow: function () {
    wx.hideHomeButton();
    var list = wx.getStorageSync('message');
    if (list.length > 0) {
      for (var i = 0; i < list.length; i++) {
        if (list[i].status == 1) {
          this.setData({
            status: 1
          })
        } else {
          this.setData({
            status: 0
          })
          break;
        }
      }
    }
    // this.getMessage();
    this.getInfo();
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  //去设置
  goInstall() {
    wx.navigateTo({
      url: '../install/install',
    })
  },
  //去消息中心
  goMessage() {
    wx.navigateTo({
      url: '../message/message',
    })
  },
  //是否有未读的公告
  getMessage() {
    var that = this;
    app.ajax({
      url: 'Partner/Message/messageList',
      data: {
        "page": 1,
        "row": 100
      },
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            is_new: res.data.data.is_new,
          })
        }

      },
    })
  },
  //登录后获取用户信息
  getInfo() {
    var that = this;
    app.ajax({
      url: 'Salesman/Index/index',
      data: {},
      success: function (res) {
        if (res.data.code == 1000) {
          that.setData({
            index_data: res.data.data,
          })
        }
      }
    })
  },


  //切换自定义tabbar
  tabbarSwitch(e) {
    console.log(e);
    const {
      index,
      pagePath: path
    } = e.detail;
    const current = this.data.current;
    if (index == current) return;
    wx.redirectTo({
      url: path
    })
  }
})